package com.ljs.springbootinit.mq;
import com.rabbitmq.client.*;

public class DirectConsumer {

  private static final String EXCHANGE_NAME = "direct-exchange";

  public static void main(String[] argv) throws Exception {
    com.rabbitmq.client.ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    com.rabbitmq.client.Connection connection = factory.newConnection();
    com.rabbitmq.client.Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "direct");
      //创建队列
      String queueName1 = "xiaowang_queue";
      channel.queueDeclare(queueName1, true, false, false, null);
      channel.queueBind(queueName1, EXCHANGE_NAME, "xiaowang");
    //创建队列
      String queueName2 = "xiaoli_queue";
      channel.queueDeclare(queueName2, true, false, false, null);
      channel.queueBind(queueName2, EXCHANGE_NAME, "xiaoli");

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    DeliverCallback xiaowangdeliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" [小王] Received '" +
            delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
    };
    DeliverCallback xiaolideliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" [小李] Received '" +
            delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
    };
    channel.basicConsume(queueName1, true, xiaowangdeliverCallback, consumerTag -> { });
   channel.basicConsume(queueName2, true, xiaolideliverCallback, consumerTag -> { });
  }
}